﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject.Resource;
using Jalal.Library.Utility;
using BCGC.BLL.BusinessObject;
using System.Drawing;
using System.Collections.Generic;

public partial class Admin_Resource_Trailer_AddTrailer : AdminBasePage
{
    List<string> uploadImage;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        ddlGameTitle.DataSource = Game.GetAllGames();
        ddlGameTitle.DataTextField = "GameTitle";
        ddlGameTitle.DataValueField = "GameId";
        ddlGameTitle.DataBind();

        ddlGameTitle.Items.Add(new ListItem("Create New Game", "NewGame"));
        ddlGameTitle.Items.Insert(0, new ListItem("Select Game", "-1"));
        ddlGameTitle.SelectedIndex = 0;

        pnlAddGame.Visible = false;
    }

    protected void AddNewGame_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (Game.Create(Security.BindStringInput(tbxNewGame.Text, SecurityLevel.Three)))
            {
                uccResponse.SetSuccessMessage("Game successfuly created");
                ClearInput();
                LoadData();
            }
            else
                uccResponse.SetFailMessage("Failed to create game");
        }
    }

    protected void GameTitle_Changed(object sender, EventArgs e)
    {
        if (ddlGameTitle.SelectedValue == "NewGame")
            pnlAddGame.Visible = true;
        else
            pnlAddGame.Visible = false;
    }

    protected void Create_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (Trailer.Create(Security.BindStringInput(tbxTitle.Text, SecurityLevel.Three),
                Security.BindIntegerInput(ddlGameTitle.SelectedValue, SecurityLevel.Three),
                Security.BindStringInput(fckDescription.Value, SecurityLevel.Three),
                Security.BindStringInput(UploadTrailerFile(), SecurityLevel.Three),
                Security.BindStringInput(((uploadImage == null || uploadImage.Count < 2) ? null : uploadImage[0]), SecurityLevel.Three),
                Security.BindStringInput(((uploadImage == null || uploadImage.Count < 2) ? null : uploadImage[1]), SecurityLevel.Three),
                Security.BindIntegerInput(tbxWidth.Text, SecurityLevel.Three),
                Security.BindIntegerInput(tbxHeight.Text, SecurityLevel.Three),
                cbxActive.Checked))
            {
                uccResponse.SetSuccessMessage("Trailer successfuly created");
                ClearInput();
            }
            else
                uccResponse.SetFailMessage("Failed to create trailer");
        }
    }

    private string UploadTrailerFile()
    {
        if (fluTrailerFile.HasFile)
        {
            return Trailer.UploadTrailerFile(fluTrailerFile.PostedFile);
        }
        else
            return null;
    }

    private void UploadTrailerImageFile()
    {
        if (uccImageUploader.HasFile)
        {
            List<Size> sizes = new List<Size>();
            sizes.Add(new Size(100, 0));
            sizes.Add(new Size(int.Parse(tbxWidth.Text.Trim()), 0));

            uccImageUploader.Sizes = sizes;
            uploadImage = uccImageUploader.UploadImage();
        }
    }

    private void ClearInput()
    {
        tbxTitle.Text = string.Empty;
        ddlGameTitle.SelectedIndex = 0;
        fckDescription.Value = string.Empty;
        cbxActive.Checked = false;
    }
}
